﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace SubTitleAddon
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        SubtitleManager mSubtitleManager;

        public MainWindow()
        {
            InitializeComponent();
            mSubtitleManager = new SubtitleManager(Display);
        }

        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Load_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Srt Files(*.srt)|*.srt|All Files(*.*)|*.*";
            if (dlg.ShowDialog(this) == true)
            {
                mSubtitleManager.Read(dlg.FileName);
                mSubtitleManager.Start();
                slider.Value = 0;
                slider.Maximum = mSubtitleManager.TotalMilliseconds;
                slider.SmallChange = (slider.Maximum - slider.MinHeight) * 0.01;
                slider.LargeChange = slider.SmallChange * 5;
            }
        }

        private void Display(Subtitle sub)
        {
            Subtitle.Dispatcher.Invoke(new Action(
                delegate() 
                { 
                    Subtitle.Text = sub.Title;
                    slider.Value = sub.Start.TotalMilliseconds;
                }));
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (mSubtitleManager != null)
              mSubtitleManager.Jump(new TimeSpan((long)(slider.Value / (slider.Maximum - slider.Minimum) * mSubtitleManager.TotalMilliseconds * 10000)));
        }
    }
}
